/**
 * 
 */
package com.exject.android.socialphonebook.view.component;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.exject.android.socialphonebook.R;

/**
 * @author Benjamin Nagel <benjamin.nagel@stud.fh-dortmund.de>
 */
public class TextInputDialog extends Dialog {
    private boolean okButtonIsSet = false;
    private boolean cancelButtonIsSet = false;
    
    /**
     * @param context
     * @param titleRessource
     *            the ressource id of the title
     */
    public TextInputDialog(Context context, int titleRessource,
            int imageRessource) {
        super(context);
        this.setTitle(titleRessource);
        this.setContentView(R.layout.text_input_dialog);
        
        TextView description = (TextView) this
                .findViewById(R.id.textInputDialogMessage);
        description.setText(titleRessource);
        
        ImageView image = (ImageView) this
                .findViewById(R.id.textInputDialogImage);
        image.setImageResource(imageRessource);
    }
    
    @Override
    public void show() {
        String exceptionMessage = "";
        if(this.okButtonIsSet == false) {
            exceptionMessage += "Set OnClickListener for the OK Button with the method setOKButtonActionListener.";
        }
        if(this.cancelButtonIsSet == false) {
            exceptionMessage += "Set OnClickListener for the Cancel Button with the method setCancelButtonActionListener.";
        }
        if(this.okButtonIsSet == false || this.cancelButtonIsSet == false) {
            throw new RuntimeException(exceptionMessage);
        }
        super.show();
    }
    
    public void setOKButtonActionListener(View.OnClickListener onClickListener) {
        this.okButtonIsSet = true;
        Button okButton = (Button) this
                .findViewById(R.id.textInputDialogOKButton);
        okButton.setOnClickListener(onClickListener);
    }
    
    public void setCancelButtonActionListener(
            View.OnClickListener onClickListener) {
        this.cancelButtonIsSet = true;
        Button cancelButton = (Button) this
                .findViewById(R.id.textInputDialogCancelButton);
        cancelButton.setOnClickListener(onClickListener);
    }
    
    public String getInput() {
        EditText inputFiled = (EditText) this
                .findViewById(R.id.textInputDialogInputField);
        return inputFiled.getText().toString();
    }
}
